<?php
require_once(dirname(__FILE__).'/../include/config.inc.php');
require_once(dirname(__FILE__).'/../include/mysqli.class.php');
session_start();
if(empty($_SESSION['adminMerchant'])){
    header("Location: /xiaochengxu/merchant");
}
$merchantId=$_SESSION['adminMerchant']['id'];
//$merchantId=527;
if(!empty($_POST['request'])){
    $sql="select a.*,b.add_place,b.add_brand,b.add_link,b.add_mobile,c.username from dyspw_goods_order as a left join dyspw_mem_address as
 b on a.add_id = b.id left join dyspw_member as c on c.id = a.shop_id where a.shop_id = ".$merchantId ."  and a.order_type = 'waimai' 
 and a.order_details is  not null order by a.time desc";
    $dopage->GetPage($sql);
    $data=array();
    while($orderInfo = $dosql->GetArray()) {
        $orderInfo['time']=date('Y-m-d H:i:s',$orderInfo['time']);
        $orderInfo['jsonInfo']=json_encode($orderInfo,JSON_UNESCAPED_UNICODE);
        $data[]=$orderInfo;
    }
    $dosql->Execute($sql);
    $returnData=array(
        'code'=>0,
        'count'=>$dosql->GetTotalRow(),
        'data'=>$data
    );
    echo json_encode($returnData,JSON_UNESCAPED_UNICODE);
    exit;
}else{
    file_put_contents($globalConfig['orderTips'],'');
}

?>

<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
    <title>商户订单管理中心</title>
    <script src="../js/jquery-2.2.4.min.js" type="text/javascript"></script>
    <script src="../js/layer/layer.js" type="text/javascript"></script>
    <script src="../css/merchant/layer/layer.js" type="text/javascript"></script>
    <script src="../css/merchant/layui/layui.js" type="text/javascript"></script>
    <script src="../css/merchant/print.js" type="text/javascript"></script>
    <link href="../css/merchant/reset.css" rel="stylesheet" type="text/css" />
    <link href="../css/merchant/left.css" rel="stylesheet" type="text/css" />
    <link href="../css/merchant/layui/css/layui.css" rel="stylesheet" type="text/css" />
</head>
<body>

<!-- 左侧导航菜单 -->
<div class="left_nav">
    <p>后台管理</p>
    <ul class="layui-nav layui-nav-tree" lay-shrink="all" id="LAY-system-side-menu" lay-filter="layadmin-system-side-menu">
        <li data-name="home" class="layui-nav-item layui-nav-itemed">
            <a href="javascript:;"><cite>订单管理</cite><span class="layui-nav-more"></span></a>
            <dl class="layui-nav-child">
                <dd class="layui-this"> <a target="right">外卖订单</a></dd>
                <dd> <a href="/xiaochengxu/uploads/setup.exe">下载插件</a></dd>
            </dl>
        </li>
    </ul>
</div>

<!-- 右上导航 -->
<div class="layui-header">
    <ul class="layui-nav layui-layout-left">
        <li class="layui-nav-item"><a href="javascript:;" onclick="window.location.reload(true)"><font class="tipShow" style="color:red;display: none">有新的订单，请点击 </font>刷新</a></li>
    </ul>
    <ul class="layui-nav layui-layout-right" lay-filter="layadmin-layout-right">
        <li class="layui-nav-item"> <a href="javascript:;"><cite><?php echo $_SESSION['adminMerchant']['username']?>商户</cite></a> </li>
<!--        <li class="layui-nav-item"> <a href="/" >退出</a> </li>-->
    </ul>
</div>

<!-- 右下iframe -->
<div class="layui-fluid">
    <div class="layui-card">
        <div id="my_form" style="padding: 20px 20px 13px">
            <table class="layui-hide" id="banner_table" lay-filter="banner_table"></table>
        </div>

        <script>
            sse();
            // start_print();
            layui.use(['form','table'], function() {
                var form = layui.form,table = layui.table;

                // 操作内容模板
                var tplOperate = function (d) {
                    var str="'"+ d.order_details+"'";
                    var html= '<div class="opetate">';
                    html +="<input type='hidden' name='goodsList' value='"+d.jsonInfo+"'>";
                    html +='<a class="layui-btn layui-btn-xs edit" onclick="orderInfo(this)">订单详情</a>';
                    html +='<a class="layui-btn layui-btn-xs" onclick="printOder(this)">打印订单</a>';
                    return html+'</div>';
                };

                table.render({
                    elem: '#banner_table'
                    ,url:'/xiaochengxu/merchant/order.php'
                    ,where:{'request':true}
                    , cols: [[
                         {field: 'add_link',align:'center',title: '联系人',width:120}
                        , {field: 'add_mobile',align:'center',title: '联系方式',width:150}
                        , {field: 'add_place',align:'left', title: '收货地址',width:250}
                        , {field: 'add_brand',align:'center', title: '门牌号',width:120}
                        , {field: 'ordernum',align:'center', title: '订单号',width:170}
                        , {field: 'time',align:'center', title: '下单时间',width:170}
                        , {field: 'operate', title: '操作', templet: tplOperate, align: 'center'}
                    ]]
                    ,loading:true
                    ,page: {
                        layout: ['prev', 'page', 'next', 'skip','count']
                        ,groups: 5
                    }
                    ,limit:20
                    ,id: 'banner_table'
                });
            });

            /*
            查看订单详情
             */
            function orderInfo(e) {
                var jsonInfo=$(e).parent().find('input[name="goodsList"]').val();
                arrayInfo=JSON.parse(jsonInfo);
                var goodsInfo=JSON.parse(arrayInfo.order_details);
                var content='<div style="padding: 20px;letter-spacing: 1px;line-height: 23px;">';
                $.each(goodsInfo,function (index,val) {
                    console.log(val)
                    content+='<div style="margin:0 0 15px;height:auto;overflow:hidden"><img style="float: left" ' +
                        'width="120" height="90" src="../'+val.img+'">' +
                        '<p style="float: left;margin-left: 15px">'+val.title+'<br>'+val.details+'<br>数量 *'+val.num+'<br>单价 '+val.money+'元</p>' +
                        '</div><hr>';
                });
                content+='备注：'+arrayInfo.remarks;
                content+='<br>包装费：'+arrayInfo.pack_fee+'元';
                content+='<br>配送费：'+arrayInfo.delivery_fee+'元';
                content+='<br>总计：'+arrayInfo.Totalprice+'元';
                content+='<br>送货地点：'+arrayInfo.add_place;
                content+='<br>联系人：'+arrayInfo.add_link;
                content+='<br>联系电话：'+arrayInfo.add_mobile;
                content+='<br>订餐时间：'+arrayInfo.time;
                content+='<br>订单编号：'+arrayInfo.ordernum;
                content +='</div>';
                layer.open({
                    type: 1,
                    title: '订单详情',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['600px', '50%'],
                    offset:'100px',
                    content: content
                });
            }

            /*
            打印订单
             */
            function printOder(e) {
                var jsonInfo=$(e).parent().find('input[name="goodsList"]').val();
                start_print(jsonInfo);
            }


            /*
            长连接
             */
            function sse() {
                var Source = new EventSource('https://5p8.fyh5p8.com/xiaochengxu/api/order.php?action=orderMessage');
                Source.onopen = function() {
                    console.log("和服务器建立连接");
                };

                Source.onmessage = function (event) {
                    if(event.data !== 'false'){
                        $(".tipShow").show();
                    }
                };
                Source.onerror = function (event) {
                    console.log('通信出错');
                };
            }
</script>
    </div>
</div>
</body></html>